package com.meraklisina.latexeditor.util;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle;
import javax.swing.UIManager;

import com.meraklisina.latexeditor.AutoSave;
import com.meraklisina.latexeditor.i18n;
import com.meraklisina.latexeditor.mdi.TextEditor.TextEditors;

public class ConfigurationGUI extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	/* Word count variables */
	private String mikTexFilePath; // Erdem
	private String languageId; // Erdem
	private String[] languageList = { "English", "Turkce" }; // Erdem

	private String autoSaveInterval; // Ali
	/* GUI components */
	private JButton closeButton;
	private JLabel totalLabel; // erdem
	private JTextField totalTextField; // erdem
	private JButton cancelButton; // Ali
	private JTextField IntMin; // Ali
	private JLabel Interval; // Ali
	private JCheckBox AutoSave; // Ali
	private JDialog dialog; // TG
	private JTabbedPane tab_Editors; // TG

	private JComboBox languageCombo; // erdem
	private JLabel languageLabel; // erdem
	private JLabel messageLabel; // erdem

	public ConfigurationGUI(JDialog jd, JTabbedPane editors) {
		super();
		initGUI(jd, editors);
	}

	public ConfigurationGUI(String mikTexPath, JDialog jd, JTabbedPane editors) {
		this(jd, editors);
		mikTexFilePath = mikTexPath;
		totalTextField.setText(mikTexFilePath);
	}

	/* Initialisation of all GUI components */
	public void initGUI(JDialog dialog, JTabbedPane editors) {
		this.dialog = dialog;
		this.tab_Editors = editors;
		this.setFocusable(true);

		Interval = new JLabel(); // Ali
		Interval.setText("AutoSave Interval (Minute)");

		AutoSave = new JCheckBox(); // Ali
		AutoSave.setText("AutoSave");

		addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(java.awt.event.KeyEvent evt) {
				formKeyPressed(evt);
			}
		});

		totalLabel = new JLabel("MikTexPath:");
		totalLabel.setName("totalWordsLabel");

		// erdem
		languageLabel = new JLabel("Language");
		languageCombo = new JComboBox(languageList);
		languageCombo.setSelectedItem(i18n.GetLanguagePreference());

		totalTextField = new JTextField("");
		totalTextField.setEditable(true);
		messageLabel = new JLabel(
				"*Lanuage changes will be applied at the next run.");

		closeButton = new JButton("OK");
		closeButton.setName("OKButton");

		cancelButton = new JButton("Cancel"); // Ali
		cancelButton.setName("CancelButton");

		IntMin = new JTextField(); // Ali
		AutoSave AutoSaveInt = new AutoSave();
		IntMin.setText(AutoSaveInt.getAutoSaveInterval());
		IntMin.setEditable(AutoSaveInt.getAutoSaveClick());

		this.AutoSave.setSelected(AutoSaveInt.getAutoSaveClick());

		AutoSave.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				AutoSaveActionPerformed(evt);
			}
		});// Ali

		cancelButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});// Ali

		closeButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				closeButtonActionPerformed(evt);
			}
		});

		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setVerticalGroup(layout.createSequentialGroup()
				.addContainerGap().addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE).addComponent(
								totalTextField, GroupLayout.Alignment.BASELINE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE).addComponent(
								totalLabel, GroupLayout.Alignment.BASELINE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE)).addGap(21)
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE).addComponent(
								languageCombo, GroupLayout.Alignment.BASELINE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE).addComponent(
								languageLabel, GroupLayout.Alignment.BASELINE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE)).addGap(21)
				.addComponent(messageLabel).addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE).addComponent(
								AutoSave, GroupLayout.Alignment.BASELINE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE).addComponent(
								Interval, GroupLayout.Alignment.BASELINE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE)).addPreferredGap(
						LayoutStyle.ComponentPlacement.UNRELATED).addComponent(
						IntMin, GroupLayout.PREFERRED_SIZE, 19,
						GroupLayout.PREFERRED_SIZE).addGap(22).addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE).addComponent(
								closeButton, GroupLayout.Alignment.BASELINE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE).addComponent(
								cancelButton, GroupLayout.Alignment.BASELINE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.PREFERRED_SIZE)).addContainerGap(
						18, 18));
		layout.setHorizontalGroup(layout.createSequentialGroup()
				.addContainerGap().addGroup(
						layout.createParallelGroup().addGroup(
								GroupLayout.Alignment.LEADING,
								layout.createSequentialGroup().addComponent(
										cancelButton,
										GroupLayout.PREFERRED_SIZE, 95,
										GroupLayout.PREFERRED_SIZE).addGap(27))
								.addComponent(AutoSave,
										GroupLayout.Alignment.LEADING,
										GroupLayout.PREFERRED_SIZE, 122,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(totalLabel,
										GroupLayout.Alignment.LEADING,
										GroupLayout.PREFERRED_SIZE, 120,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(languageLabel,
										GroupLayout.Alignment.LEADING,
										GroupLayout.PREFERRED_SIZE, 120,
										GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addGroup(
						layout.createParallelGroup().addGroup(
								GroupLayout.Alignment.LEADING,
								layout.createSequentialGroup().addComponent(
										closeButton,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE).addGap(0,
										130, Short.MAX_VALUE)).addComponent(
								Interval, GroupLayout.Alignment.LEADING, 0,
								161, Short.MAX_VALUE).addComponent(
								totalTextField, GroupLayout.Alignment.LEADING,
								0, 161, Short.MAX_VALUE).addComponent(
								languageCombo, GroupLayout.Alignment.LEADING,
								0, 161, Short.MAX_VALUE).addComponent(
								messageLabel, GroupLayout.Alignment.LEADING, 0,
								161, Short.MAX_VALUE).addGroup(
								GroupLayout.Alignment.LEADING,
								layout.createSequentialGroup().addComponent(
										IntMin, 0, 58, Short.MAX_VALUE).addGap(
										103))).addContainerGap());
	}

	public JTextPane getTextEditor() {
		return new TextEditors().GetJTextPaneAt(this.tab_Editors,
				this.tab_Editors.getSelectedIndex());
	}

	private void formKeyPressed(java.awt.event.KeyEvent evt) {// GEN-FIRST:event_formKeyPressed
		// TODO add your handling code here:
	}// GEN-LAST:event_formKeyPressed

	private void closeButtonActionPerformed(ActionEvent actionEvent) {// GEN-FIRST:event_BtnCancelActionPerformed
		mikTexFilePath = totalTextField.getText();
		if (mikTexFilePath != null) {
			try {
				FileWriter fileWriter;
				fileWriter = new FileWriter("config.txt");
				BufferedWriter out = new BufferedWriter(fileWriter);
				out.write(mikTexFilePath + "\n");
				autoSaveInterval = IntMin.getText();
				if (AutoSave.isSelected()) {
					out.write("Auto" + "\n");
					if (autoSaveInterval != null) {
						out.write(autoSaveInterval);
					}
				} else {
					out.write("Manuel" + "\n");
					out.write("0");
				}

				// save language setting - erdem
				out.write("\nLanguage\n" + languageCombo.getSelectedItem());

				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else {
			JOptionPane.showMessageDialog(this, "No Opened File ", "Error: ",
					JOptionPane.ERROR_MESSAGE);
		}
		this.dialog.setVisible(false);
	}

	private void AutoSaveActionPerformed(ActionEvent actionEvent) {// GEN-FIRST:event_BtnCancelActionPerformed
		if (AutoSave.isSelected()) {
			IntMin.setEditable(true);
		} else {
			IntMin.setEditable(false);
			IntMin.setText("0");
		}
	}

	private void cancelButtonActionPerformed(ActionEvent actionEvent) {// GEN-FIRST:event_BtnCancelActionPerformed
		this.dialog.setVisible(false);
	}

	public void actionPerformed(ActionEvent event) {
		/* Count Button Handler */

	}

}
